package com.wnnight.wys.mapper.landlord;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wnnight.wys.entity.lanlord.Landlord;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.FetchType;

/**
 * @author Mr.Wang
 * @ClassName: Landlord
 * @Description: 业主mapper
 * @date: 2022/8/29-10:02
 */
public interface LandlordMapper extends BaseMapper<Landlord> {

    @UpdateProvider(type = LandlordSql.class, method = "update")
    void update(Landlord landlord);

    @Update("update wys_landlord set name=#{name},phone=#{phone},gender=#{gender},id_card=#{idCard},place=#{place},status='s',sub_time=#{subTime} where id=#{id} and status='n'")
    void realName(Landlord landlord);

    @Select("select * from wys_landlord where account=#{account} and password=#{password}")
    Landlord login(@Param("account") String account,@Param("password") String password);

    @Select("select * from wys_landlord where id=#{id}")
    Landlord info(int id);

    @Select("select account,img from wys_landlord where id=#{id}")
    Landlord getImg(int id);

    @Select("select count(*) from wys_landlord where ${column}=#{value}")
    int getNumByColumn(@Param("column") String column,@Param("value") String value);

    //逻辑删除，x标识已删除
    @Update("update wys_landlord set status='x' where id=#{id}")
    void deleteLandlord(int id);

    //查询业主房屋信息
    @Select("select * from wys_landlord where id=#{id}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id_card",property = "idCard"),
            @Result(column = "sub_time",property = "subTime"),
            @Result(column = "id",property = "rooms",many = @Many(select = "com.wnnight.wys.mapper.room.RoomMapper.getByLandlordId",fetchType = FetchType.LAZY)),
    })
    Landlord getRoomInfo(int id);
    //查询业主车位信息
    @Select("select * from wys_landlord where id=#{id}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id_card",property = "idCard"),
            @Result(column = "sub_time",property = "subTime"),
            @Result(column = "id",property = "parks",many = @Many(select = "com.wnnight.wys.mapper.car.ParkMapper.getByLandlordId",fetchType = FetchType.LAZY)),
    })
    Landlord getParkInfo(int id);
    //查询业主车辆信息
    @Select("select * from wys_landlord where id=#{id}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id_card",property = "idCard"),
            @Result(column = "sub_time",property = "subTime"),
            @Result(column = "id",property = "cars",many = @Many(select = "com.wnnight.wys.mapper.car.CarMapper.getByLandlordId",fetchType = FetchType.LAZY)),
    })
    Landlord getCarInfo(int id);



//    @SelectProvider(type = LandlordSql.class, method = "selectByCondition")
//    List<Landlord> getByCondition(Landlord landlord);


    //业主mapper动态sql
    class LandlordSql {
        //修改
        public static String update(Landlord landlord) {
            String sql = new SQL() {
                {
                    UPDATE("wys_landlord");
                    if (landlord.getName() != null && !landlord.getName().trim().equals("")) {
                        SET("name=#{name}");
                    }
                    if (landlord.getPhone() != null && !landlord.getPhone().trim().equals("")) {
                        SET("phone=#{phone}");
                    }
                    if (landlord.getGender() != null && !landlord.getGender().trim().equals("")) {
                        SET("gender=#{gender}");
                    }
                    if (landlord.getPlace() != null && !landlord.getPlace().trim().equals("")) {
                        SET("place=#{place}");
                    }
                    if (landlord.getIdCard() != null && !landlord.getIdCard().trim().equals("")) {
                        SET("id_card=#{idCard}");
                    }
                    if (landlord.getAccount() != null && !landlord.getAccount().trim().equals("")) {
                        SET("account=#{account}");
                    }
                    if (landlord.getPassword() != null && !landlord.getPassword().trim().equals("")) {
                        SET("password=#{password}");
                    }
                    if (landlord.getStatus() != null && !landlord.getStatus().trim().equals("")) {
                        SET("status=#{status}");
                    }
                    if (landlord.getImg() != null && landlord.getImg().length>0) {
                        SET("img=#{img}");
                    }
                    if (landlord.getSubTime() != null && !landlord.getSubTime().trim().equals("")) {
                        SET("sub_time=#{subTime}");
                    }
                    WHERE("id=#{id}");
                }
            }.toString();
            return sql;
        }


//        //多条件查询
//        public static String selectByCondition(Landlord landlord) {
//            String sql = new SQL() {{
//                SELECT("*");
//                FROM("wys_landlord");
//                if (landlord.getName() != null && !landlord.getName().trim().equals("")) {
//                    WHERE("name like '%"+landlord.getName()+"%'");
//                }
//                if (landlord.getPhone()!=null && !landlord.getPhone().trim().equals("")) {
//                    WHERE("phone like '%"+landlord.getPhone()+"%'");
//                }
//                if (landlord.getStatus()!=null && !landlord.getStatus().trim().equals("")) {
//                    WHERE("status=#{status}");
//                }
//                if (landlord.getPlace()!=null && !landlord.getPlace().trim().equals("")) {
//                    WHERE("place like '%"+landlord.getPlace()+"%'");
//                }
//                if (landlord.getMinSubTime()!=null && !landlord.getMinSubTime().trim().equals("")) {
//                    WHERE("sub_time &gt;= #{minSubTime}");
//                }
//                if (landlord.getMaxSubTime()!=null && !landlord.getMaxSubTime().trim().equals("")) {
//                    WHERE("sub_time &lt;= #{maxSubTime}");
//                }
//            }}.toString();
//            return sql;
//        }

    }

}
